package statemodel.impl;

import lombok.Getter;
import statemodel.base.BaseState;
import statemodel.entity.NewGumballMachine;

/**
 * @author yaojianfeng
 */
public class WinnerStateImpl implements BaseState {
    @Getter
    private String desc = "中奖了,出两颗糖果";
    private NewGumballMachine gumballMachine;

    /**
     * 我们通过构造器得到糖果机的引用,然后将它记录在实例变量中
     * @param gumballMachine 糖果机对象
     */
    public WinnerStateImpl(NewGumballMachine gumballMachine) {
        this.gumballMachine = gumballMachine;
    }

    @Override
    public void insertQuarter() {
        System.out.println("恭喜中奖 无需投币");
    }

    @Override
    public void ejectQuarter() {
        System.out.println("不投币 你还想退币? 你要白嫖是吧?");
    }

    @Override
    public void turnCrank() {
        System.out.println("曲柄自己会转动的!");
    }

    @Override
    public void dispense() {
        System.out.println("你可真是一个幸运儿!一次投币,竟然得到了两颗糖果!");
        gumballMachine.releaseBall();
        if (gumballMachine.getCount()<=0){
            gumballMachine.setState(gumballMachine.getSoldOutState());
        }else {
            gumballMachine.releaseBall();
            if (gumballMachine.getCount()>0){
                gumballMachine.setState(gumballMachine.getNoQuarterState());
            }else {
                System.out.println("啊哈,糖果没有啦");
                gumballMachine.setState(gumballMachine.getSoldOutState());
            }
        }
    }
}
